#!/bin/bash

## Should work on Linux
SHORTHOST=$(hostname --short)
## Workaround
# SHORTHOST="somehost"

if [ -z $1 ]
then NUMNODES=20
else NUMNODES=$1
fi

echo "Compiling db2..."
cd db2
erl -make

echo "Compiling testbed..."
cd ../testbed
mkdir ebin
erl -make

cd ..

echo "Starting GUI in background..."
erl -smp -pa db2/ebin testbed/ebin -setcookie apa -sname gui -s db_gui start -noshell &

echo "Launching worker nodes..."
ruby launch_nodes_sname.rb $NUMNODES gui@$SHORTHOST

echo "Starting master in terminal..."
xterm -e erl -pa db2/ebin testbed/ebin -setcookie apa -sname master -s net_adm ping_list gui@$SHORTHOST

echo "Asking beam to terminate..."
killall beam.smp
killall beam

echo "Waiting for beam to die..."
sleep 2

echo "Killing beam..."
killall -9 beam.smp
killall -9 beam

